package com.garrett.elasticsearch.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class ElasticSearchConfig {
    /**
     * ES依赖TransportClient
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public TransportClient client() throws UnknownHostException {
        //构造ES地址，端口不能写9200，默认ES的TCP端口应该为9300
        InetSocketTransportAddress node=new InetSocketTransportAddress(
                InetAddress.getByName("192.168.214.128"),9300
        );

        //Settings是ES Client的一个配置
        Settings settings = Settings.builder().put("cluster.name", "chenyulin").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        //可以执行很多个addTransportAddress
        client.addTransportAddress(node);
        return client;
    }
}
